package com.huluobo.user.view

import android.text.TextUtils
import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.UIState
import com.huluobo.lib_base.entity.User
import com.huluobo.user.UserIntent
import com.huluobo.user.UserViewModel
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/3/3.
 */

@Composable
fun RegisterPage(navHostController: NavHostController, vm: UserViewModel = hiltViewModel()) {
    LaunchedEffect(key1 = "", block = {

        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnError -> {
                        Log.e("TagA", "注册用户失败:${it.msg}")
                    }

                    UIState.OnLoading -> {}
                    is UIState.OnSuccess<*> -> {
                        Log.i("TagA", "注册成功:${it.data.toString()}")
                        navHostController.popBackStack()
                    }
                }
            }
        }
    })

    var username by remember {
        mutableStateOf("")
    }

    var password by remember {
        mutableStateOf("")
    }

    var rePassword by remember {
        mutableStateOf("")
    }

    var role by remember {
        mutableStateOf("员工")
    }

    Column(verticalArrangement = Arrangement.spacedBy(20.dp), modifier = Modifier.padding(20.dp)) {
        BaseTitle(title = "注册", navHostController = navHostController)
        Spacer(modifier = Modifier.height(100.dp))
        OutlinedTextField(value = username, onValueChange = { username = it }, Modifier.fillMaxWidth(), label = {
            Text(text = "用户名")
        }, leadingIcon = { Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "") })
        OutlinedTextField(
            value = password,
            onValueChange = { password = it },
            Modifier.fillMaxWidth(),
            label = {
                Text(text = "密码")
            },
            leadingIcon = { Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "") },
            visualTransformation = PasswordVisualTransformation()
        )
        OutlinedTextField(
            value = rePassword,
            onValueChange = { rePassword = it },
            Modifier.fillMaxWidth(),
            label = {
                Text(text = "确认密码")
            },
            leadingIcon = { Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "") },
            visualTransformation = PasswordVisualTransformation()
        )

        Row(verticalAlignment = Alignment.CenterVertically) {
            Text(text = "角色 : ")
            Checkbox(checked = role == "经理", onCheckedChange = {
                if (it) {
                    role = "经理"
                }
            })
            Text(text = "经理")
            Checkbox(checked = role == "员工", onCheckedChange = {
                if (it) {
                    role = "员工"
                }
            })
            Text(text = "员工")
        }

        Button(modifier = Modifier.fillMaxWidth(), onClick = {
            if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(rePassword)) {
                ToastUtils.showLong("输入项不能为空")
                return@Button
            }

            if (!username.matches(Regex("[A-z0-9]{3,10}"))) {
                ToastUtils.showLong("用户需要字母或者数字,3到10位")
                return@Button
            }

            if (password != rePassword) {
                ToastUtils.showLong("两次密码输入不一致")
                return@Button
            }

            vm.sendIntent(
                UserIntent.Register(
                    User(
                        username = username,
                        password = password,
                        role = if (role == "经理") 1 else 2
                    )
                )
            )
        }) {
            Text(text = "注册")
        }
    }
}